<template>
  <div class="home-banner">
    <div class="banner-container">
      <!-- 左侧分类菜单 -->
      <div class="category-menu" @mouseleave="activeIndex = null">
        <div class="menu-wrapper">
          <div
            v-for="(category, index) in categories"
            :key="index"
            class="category-item"
            @mouseenter="activeIndex = index"
          >
            <div class="category-name">{{ category.name }}</div>
            <SvgIcon type="arrow-right" />
          </div>
        </div>

        <!-- 悬浮分类面板 -->
        <div v-if="activeIndex !== null" class="hover-panel">
          <div
            v-for="(child, idx) in categories[activeIndex].children"
            :key="idx"
            class="hover-item"
          >
            {{ child.name }}
          </div>
        </div>
      </div>

      <!-- 主轮播区域 -->
      <div class="main-slider">
        <div class="banner-wrapper">
          <div
            v-for="(banner, idx) in banners"
            :key="idx"
            class="banner-item"
            :class="{ active: currentIndex === idx }"
          >
            <div class="banner-inner">
              <!-- 实际项目中使用实际图片路径 -->
              <div
                class="banner-image"
                :style="{
                  backgroundColor: banner.bgColor,
                  backgroundImage: banner.image ? `url(${banner.image})` : 'none',
                }"
              ></div>
            </div>
          </div>
        </div>

        <div class="banner-indicators">
          <div
            v-for="(banner, idx) in banners"
            :key="idx"
            class="indicator"
            :class="{ active: currentIndex === idx }"
            @click="currentIndex = idx"
          />
        </div>
      </div>

      <!-- 右侧功能区 -->
      <div class="right-panel">
        <div class="user-card">
          <div class="avatar">
            <SvgIcon type="user" />
          </div>
          <div class="login-prompt">Hi，欢迎来到E-Shop</div>
          <button class="login-btn">登录</button>
        </div>

        <div class="news-panel">
          <div class="panel-header">
            <span class="title">公告</span>
            <a href="#" class="more">更多</a>
          </div>
          <ul class="news-list">
            <li v-for="(news, idx) in newsList" :key="idx" class="news-item">
              {{ news }}
            </li>
          </ul>
        </div>
      </div>
    </div>

    <!-- 促销信息条 -->
    <div class="promo-bar">
      <div v-for="(promo, idx) in promoList" :key="idx" class="promo-item">
        <span class="icon" :style="{ backgroundColor: promo.bgColor }">
          <SvgIcon :type="promo.icon" color="#fff" />
        </span>
        <div class="text">
          <div class="title">{{ promo.title }}</div>
          <div class="desc">{{ promo.desc }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import SvgIcon from '@/components/ui/SvgIcon.vue'
import type { Promotion } from '@/types'

interface CategoryItem {
  name: string
  children: { name: string }[]
}

// 分类数据
const categories: CategoryItem[] = [
  {
    name: '手机',
    children: [
      { name: '手机整机' },
      { name: '手机配件' },
      { name: '智能设备' },
      { name: '通信服务' },
      { name: '电池/充电器' },
      { name: '手机壳/贴膜' },
    ],
  },
  {
    name: '电脑',
    children: [
      { name: '笔记本电脑' },
      { name: '品牌台式机' },
      { name: '电脑配件' },
      { name: '办公设备' },
      { name: '网络设备' },
      { name: '外设产品' },
    ],
  },
  {
    name: '数码',
    children: [
      { name: '相机摄像机' },
      { name: '智能设备' },
      { name: '影音娱乐' },
      { name: '电子教育' },
      { name: '智能穿戴' },
      { name: '数码配件' },
    ],
  },
  {
    name: '家电',
    children: [
      { name: '大家电' },
      { name: '厨卫大电' },
      { name: '厨卫小电' },
      { name: '生活电器' },
      { name: '个护健康' },
      { name: '家电配件' },
    ],
  },
  {
    name: '食品',
    children: [
      { name: '新鲜果蔬' },
      { name: '奶品烘培' },
      { name: '肉类水产' },
      { name: '粮油调味' },
      { name: '酒水饮料' },
      { name: '方便速食' },
    ],
  },
  {
    name: '服装',
    children: [
      { name: '男装' },
      { name: '女装' },
      { name: '内衣' },
      { name: '鞋靴' },
      { name: '箱包' },
      { name: '配件' },
    ],
  },
]

// 轮播图数据
const banners = [
  { title: '夏季节日大促', desc: '全场低至5折起', bgColor: '#FFECB3', image: '' },
  { title: '新机首发', desc: '旗舰新品限时优惠', bgColor: '#D7CCC8', image: '' },
  { title: '清凉家电节', desc: '空调电扇热卖中', bgColor: '#C5E1A5', image: '' },
  { title: '数码特惠', desc: '摄影装备立减300', bgColor: '#80DEEA', image: '' },
  { title: '运动健康装备', desc: '提升你的健身效率', bgColor: '#F8BBD0', image: '' },
]

// 公告列表
const newsList = [
  '【最新公告】积分商城升级，好礼等你兑换',
  '【促销】7月清凉家电节，全平台满减活动',
  '【新品】新款智能手表上线，预订享好礼',
  '【物流】新增多城市次日达服务',
]

// 促销信息
const promoList: Promotion[] = [
  { title: '品质保障', desc: '精选品牌 正品好货', icon: 'shield', bgColor: '#FF9800' },
  { title: '价格优惠', desc: '满300减40 多买多减', icon: 'tag', bgColor: '#F44336' },
  { title: '闪电物流', desc: '今日下单 明日送达', icon: 'truck', bgColor: '#4CAF50' },
  { title: '无忧售后', desc: '7天无理由退换', icon: 'rotate', bgColor: '#2196F3' },
]

const activeIndex = ref<number | null>(null)
const currentIndex = ref(0)

// 自动轮播逻辑
let timer: number
onMounted(() => {
  timer = window.setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % banners.length
  }, 5000)
})

onUnmounted(() => {
  clearInterval(timer)
})
</script>
<style scoped lang="scss">
.home-banner {
  position: relative;

  .banner-container {
    display: flex;
    height: 400px;

    /* 移动端适配 - 隐藏轮播、公告和分类菜单 */
    @media (max-width: 768px) {
      display: none !important;
    }

    .category-menu {
      position: relative;
      width: 240px;
      background: rgba(0, 0, 0, 0.8);
      color: #fff;
      z-index: 10;

      .menu-wrapper {
        padding: 15px 0;

        .category-item {
          height: 42px;
          padding: 0 20px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          font-size: 15px;
          cursor: pointer;
          transition: background-color 0.2s;

          &:hover {
            background-color: rgba(255, 255, 255, 0.2);
          }

          svg {
            width: 14px;
            height: 14px;
          }
        }
      }

      .hover-panel {
        position: absolute;
        top: 0;
        left: 240px;
        width: calc(100vw - 240px - 280px - 20px); /* 减少边距计算 */
        max-width: 880px; /* 增加最大宽度 */
        height: 400px; /* 恢复标准高度 */
        background: #fff;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        display: grid;
        grid-template-columns: repeat(6, 1fr);
        padding: 15px;
        z-index: 20;
        overflow: hidden;
        margin-right: 20px;
        box-sizing: border-box;

        .hover-item {
          padding: 10px 15px;
          color: #333;
          cursor: pointer;
          transition: color 0.3s;

          &:hover {
            color: #f56c6c;
          }
        }
      }
    }

    .main-slider {
      flex: 1;
      position: relative;
      overflow: hidden;

      .banner-wrapper {
        height: 400px;
        position: relative;

        .banner-item {
          position: absolute;
          width: 100%;
          height: 100%;
          opacity: 0;
          transition: opacity 0.8s ease-in-out;

          &.active {
            opacity: 1;
            z-index: 2;
          }

          .banner-inner {
            width: 100%;
            height: 100%;
            position: relative;

            .banner-image {
              width: 100%;
              height: 100%;
              background-size: cover;
              background-position: center;
              background-color: #eee;
            }
          }
        }
      }

      .banner-indicators {
        position: absolute;
        bottom: 20px;
        left: 50%;
        transform: translateX(-50%);
        display: flex;
        gap: 8px;
        z-index: 10;

        .indicator {
          width: 30px;
          height: 4px;
          background-color: rgba(255, 255, 255, 0.5);
          cursor: pointer;
          border-radius: 2px;
          transition: all 0.3s;

          &:hover,
          &.active {
            background-color: #fff;
            width: 40px;
          }
        }
      }
    }

    .right-panel {
      width: 260px;
      margin-left: 20px;
      display: flex;
      flex-direction: column;
      gap: 20px;

      .user-card {
        height: 150px;
        background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
        border-radius: 8px;
        color: white;
        padding: 20px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        .avatar {
          width: 60px;
          height: 60px;
          background: rgba(255, 255, 255, 0.3);
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-bottom: 10px;
        }

        .login-prompt {
          font-size: 16px;
          margin-bottom: 12px;
        }

        .login-btn {
          background: white;
          color: #e67e22;
          border: none;
          padding: 6px 25px;
          border-radius: 20px;
          font-weight: bold;
          cursor: pointer;
          transition: transform 0.2s;

          &:hover {
            transform: translateY(-2px);
          }
        }
      }

      .news-panel {
        flex: 1;
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        overflow: hidden;

        .panel-header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 15px 20px;
          border-bottom: 1px solid #f5f5f5;

          .title {
            font-weight: bold;
            font-size: 16px;
            position: relative;

            &::after {
              content: '';
              position: absolute;
              left: 0;
              bottom: -4px;
              width: 40px;
              height: 3px;
              background: linear-gradient(to right, #ff4e50, #f9d423);
            }
          }

          .more {
            color: #999;
            font-size: 14px;

            &:hover {
              color: #f56c6c;
            }
          }
        }

        .news-list {
          padding: 10px 0;

          .news-item {
            padding: 10px 20px;
            font-size: 14px;
            cursor: pointer;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            transition: all 0.2s;

            &:hover {
              background: #f5f5f5;
              color: #f56c6c;
            }
          }
        }
      }
    }
  }

  .promo-bar {
    display: flex;
    height: 70px;
    background: #fff;
    margin-top: 10px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

    /* 移动端隐藏促销信息栏 */
    @media (max-width: 768px) {
      display: none !important;
    }

    .promo-item {
      flex: 1;
      display: flex;
      align-items: center;
      padding: 0 20px;

      &:not(:last-child) {
        border-right: 1px solid #f5f5f5;
      }

      .icon {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 15px;
      }

      .text {
        .title {
          font-weight: 600;
          font-size: 16px;
          margin-bottom: 2px;
        }

        .desc {
          font-size: 13px;
          color: #888;
        }
      }
    }
  }
}
</style>
